package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.stylefeng.guns.modular.school.entity.ParkingInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceInfo;
import cn.stylefeng.guns.modular.school.model.params.ParkingPlaceInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ParkingPlaceInfoResult;
import cn.stylefeng.guns.modular.school.service.ParkingInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/parking/place")
public class ApiNeedTokenParkingPlaceController extends BaseController {

    @Autowired
    private ParkingPlaceInfoService parkingPlaceInfoService;
    @Autowired
    private ParkingInfoService parkingInfoService;

    @RequestMapping(value = "detail")
    public Object detail(Integer seqId, String openId) {
        Map map = new HashMap();

        ParkingPlaceInfo parkingPlaceInfo = parkingPlaceInfoService.getById(seqId);
        ParkingPlaceInfoResult parkingPlaceInfoResult = new ParkingPlaceInfoResult();
        ToolUtil.copyProperties(parkingPlaceInfo, parkingPlaceInfoResult);
        parkingPlaceInfoResult.setTimeStartStr(DateUtil.format(parkingPlaceInfo.getTimeStart(), "HH:mm"));
        parkingPlaceInfoResult.setTimeEndStr(DateUtil.format(parkingPlaceInfo.getTimeEnd(), "HH:mm"));
        map.put("parkingPlaceUseInfo", parkingPlaceInfoResult);
        map.put("now", FastDateFormat.getInstance("HH:mm").format(new DateTime()));
        SUCCESS_TIP.setData(map);

        return SUCCESS_TIP;
    }


    @RequestMapping(value = "list")
    public Object list(String openId) {
        Map map = new HashMap();

        List<ParkingPlaceInfo> parkingPlaceInfos = parkingPlaceInfoService.list(new QueryWrapper<ParkingPlaceInfo>().eq("open_id", openId));
        List<ParkingPlaceInfoResult> list = new ArrayList<>();
        parkingPlaceInfos.forEach(parkingPlaceInfo -> {
            ParkingInfo parkingInfo = parkingInfoService.getById(parkingPlaceInfo.getParkingId());
            ParkingPlaceInfoResult parkingPlaceInfoResult = new ParkingPlaceInfoResult();
            ToolUtil.copyProperties(parkingPlaceInfo, parkingPlaceInfoResult);
            parkingPlaceInfoResult.setParkingName(parkingInfo.getName());
            list.add(parkingPlaceInfoResult);
        });

        map.put("parkingPlaceInfos", list);
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        this.parkingPlaceInfoService.update(parkingPlaceInfoParam);
        return ResponseData.success();
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        this.parkingPlaceInfoService.add(parkingPlaceInfoParam);
        return ResponseData.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ParkingPlaceInfoParam parkingPlaceInfoParam) {
        this.parkingPlaceInfoService.delete(parkingPlaceInfoParam);
        return ResponseData.success();
    }
}


